<!--
 * @Author: 岸舟
 * @Date: 2020-09-21 11:48:02
 * @LastEditors: MiNine
 * @LastEditTime: 2020-09-23 15:42:01
 * @Description: 
-->
<template>
<!-- Sign In -->
<div class="container">
    <div class="login">
        <h2 class="title">泓 立 保 险</h2>
        <el-input placeholder="Account" class="input" v-model.lazy.trim="clientData.account" />
        <el-input show-password placeholder="Password" class="input" v-model.lazy.trim="clientData.password" />
        <el-row>
            <el-button style="width:6rem" @click="login">登&nbsp;&nbsp;&nbsp;录</el-button>
        </el-row>
    </div>
</div>
</template>

<script>
import apis from "../../http/apis/modules/entryApi"
import {
    createNamespacedHelpers
} from "vuex"
const {
    mapState,
    mapMutations
} = createNamespacedHelpers("publicStore")

export default {
    name: "login",
    data() {
        return {
            clientData: {
                account: "",
                password: ""
            },
            hint: {
                isShow: false,
                text: ""
            }
        }
    },
    methods: {
        ...mapMutations(["changeCurrentUser"]),
        // 登录方法
        async login() {
            if (this.clientData.account.includes(" ") || this.clientData.password.includes(" ")) {
                this.hint.text = "用户名或密码不能包含空格"
                this.openCenter()
            } else if (!this.clientData.account || !this.clientData.password) {
                this.hint.text = "用户名或密码不能为空"
                this.openCenter()
            } else {
                const backendVal = await apis.login(this.clientData);
                if (backendVal.data.status != 0) {
                    this.hint.isShow = "false"
                    console.log(backendVal.data);
                    //登陆成功后将用户信息存入sessstorage中
                    sessionStorage.admin_id = backendVal.data.data[0]._id;
                    sessionStorage.permission = backendVal.data.data[0].permission;
                    // 登录成功将登录用户的信息存入publicStore中
                    this.changeCurrentUser(backendVal.data.data[0]);
                    // 登录成功将token保留在本地
                    localStorage.token = backendVal.data.token;
                    this.$router.push("/system");
                } else {
                    this.hint.text = backendVal.data.msg;
                    this.openCenter();
                }
            }
        },
        // 提示框用方法
        openCenter() {
            this.$message({
                message: this.hint.text,
                center: true
            });
        }
    },
    computed: {
        ...mapState(["globalPermission"])
    }
}
</script>

<style lang="scss" scoped>
@import "../../assets/scss/Entry/Entry.scss";
</style>
